data structures greedy implementation *800

Please click on ads to support us..

Python Code:

import os
import sys
sys.setrecursionlimit(10 ** 5)
LOCAL = True
try:
    open(f"{os.path.realpath(os.path.dirname(__file__))}/../generator.py")
except:
    LOCAL = False

MOD = 10 ** 9 + 7

fib_mem = [0, 1]
def fib(n):
    while len(fib_mem) < n + 1:
        fib_mem.append((fib_mem[-1] + fib_mem[-2]) % MOD)
    return fib_mem[n]
fac_mem = [1]
def fac(n):
    while len(fac_mem) < n + 1:
        fac_mem.append(fac_mem[-1] * len(fac_mem) % MOD)
    return fac_mem[n]
def perm(n, k):
    return fac(n) * pow(fac(k), MOD - 2, MOD) % MOD
def comb(n, k):
    return fac(n) * pow(fac(k) * fac(n - k) % MOD, MOD - 2, MOD) % MOD
def count(l):
    r = {}
    for i in l:
        r[i] = r.get(i, 0) + 1
    return r
def group(l):
    r = []
    p = None
    c = 0
    for i in l:
        if i == p:
            c += 1
        else:
            r.append((p, c))
            p = i
            c = 1
    if c:
        r.append((p, c))
    return r
class Tree:
    def __init__(self, id=0):
        self.id = id
        self.parent = None
        self.children = []
    def __repr__(self):
        if self.children:
            return f"{self.id} -> [" + ", ".join(str(c) for c in self.children) + "]"
        else:
            return f"{self.id}"
def bin_search(l, a, b, g):
    if a == b:
        return a
    mid = (a + b) // 2
    if g < l[mid]:
        return bin_search(l, mid + 1, b, g)
    else:
        return bin_search(l, a, mid, g)
def ints():
    return [int(j) for j in input().split()]
def strings():
    return input().split()
def deb(*l, **kwargs):
    if LOCAL:
        print("\033[2m", end="")
        print(*l, **kwargs)
        print("\033[0m", end="")
def ans(*l, **kwargs):
    if LOCAL:
        print("\033[32m", end="")
    if l and type(l[0]) == bool:
        print(["NO", "YES"][l[0]], **kwargs)
    else:
        print(*l, **kwargs)
    if LOCAL:
        print("\033[0m", end="")


for case in range(int(input())):
    n, = ints()
    s = ints()
    f = ints()

    d = [0] * n
    t = 0

    for i in range(n):
        t = max(s[i], t)
        d[i] = f[i] - t
        t = f[i]
    
    ans(*d)

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define pp pop_back
#define sz size()
#define N 200005
#define ff first
#define ss second
#define pb push_back
using namespace std;

long long t, n, s[N], f[N];

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> t;

	while( t-- ){
		cin >> n;
		for(int i = 1; i <= n; i++){
			cin >> s[i];
		}

		for(int i = 1; i <= n; i++){
			cin >> f[i];
		}

		for(int i = 1; i <= n; i++){
			if(s[i] < f[i-1]){
				cout << abs(f[i-1]-f[i]) << ' ';
			}

			else {
				cout << abs(s[i]-f[i]) << ' ';
			}
		}
		cout << '\n';
	}
}


Comments

Submit
0 Comments
More Questions

Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!